package A1_数组;

public class LC_59螺旋矩阵II {
    public static void main(String[] args){
        System.out.println(generateMatrix(3));
    }
    public static int[][] generateMatrix(int n) {
        int startx=0,starty=0,i=0,j=0;
        int offset=1,count=1;
        int[][] nums=new int[n][n];
        int nn=1;//记录当前圈数
        while(nn<=n/2 && count<=n*n && startx<=starty && i<=j){
            for(j=starty;j<n-offset;j++)
                nums[startx][j]=count++;
            for(i=startx;i<n-offset;i++)
                nums[i][j]=count++;
            for(;j>starty;j--)
                nums[i][j]=count++;
            for(;i>startx;i--)
                nums[i][j]=count++;
            startx++;
            starty++;
            offset++;
            nn++;
        }
        if(n%2!=0)
            //nums[i+1][j+1]=count++;
            nums[startx][starty]=count++;
        return nums;
    }
}
